package com.platform.hjt.from.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.hjt.tools.dao.Page;
import com.hjt.tools.dao.PropertyFilter;
import com.hjt.tools.date.DateCreator;
import com.platform.hjt.base.controller.ReturnBody;
import com.platform.hjt.base.service.BaseService;
import com.platform.hjt.flow.service.SnakerEngineFacets;
import com.platform.hjt.from.dao.FromDefaultDataDAO;
import com.platform.hjt.from.model.FromDefaultData;

@Service
public class FromDefaultDataService extends BaseService{

	@Resource
	private FromDefaultDataDAO fromDefaultDataDAO;
	@Resource
	private SnakerEngineFacets facets;
	
	public ReturnBody saveFromDefaultData(FromDefaultData fromDefaultData){
		String status = getRequest().getParameter("status");
		
		fromDefaultData.setSysUserInfo(getSessionUserInfo());
		fromDefaultData.setCreateTime(DateCreator.getCurrentTime());
		fromDefaultDataDAO.saveJ(fromDefaultData);
		
		if(status.equals("-1")){	//扭转上节点
			facets.getEngine().executeAndJumpTask(fromDefaultData.getTaskId(), getSessionUserInfo().getSysUserInfoId(), null, null);
			return new ReturnBody(true, "流程已回退");
			
		} else if(status.equals("1")){
			facets.getEngine().executeTask(fromDefaultData.getTaskId(), getSessionUserInfo().getSysUserInfoId());
		}
		return new ReturnBody(true, "审核完成");
	}
	
	public Page<FromDefaultData> getFromDefaultDataList(Page<FromDefaultData> page, List<PropertyFilter> propertyFilterList){
		return fromDefaultDataDAO.findPage(page, propertyFilterList);
	}
	
	public List<FromDefaultData> getFromDefaultDataList(List<PropertyFilter> filters){
		return fromDefaultDataDAO.find(filters);
	}
	
	public FromDefaultData getFromDefaultData(List<PropertyFilter> filters){
		return fromDefaultDataDAO.findUniqueBy(filters);
	}
	
	public FromDefaultData getFromDefaultData(String propertyName, Object value){
		return fromDefaultDataDAO.findUniqueBy(propertyName, value);
	}
	
	@SuppressWarnings("all")
	public FromDefaultData getFromDefaultData(Map<String, Object> map){
		Criterion criterion = Restrictions.allEq(map);
		List<FromDefaultData> list = fromDefaultDataDAO.createCriteria(criterion).setMaxResults(1).addOrder(Order.desc("createTime")).list();
		if(list.isEmpty()){
			return null;
		}else{
			return list.get(0);
		}
	}
	
}
